import {
  Column,
  Entity,
  ManyToMany,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Car } from '../../car/entities/car.entity';
import { User } from '../../user/entities/user.entity';
import { Order } from '../../order/entities/order.entity';
import { Geography } from '../../geograph/entities/geograph.entity';
import { ServicePhoto } from '../../service_photo/entities/service_photo.entity';
import { v4 as uuidv4 } from 'uuid';
import { Cart } from '../../cart/entities/cart.entity';

@Entity('service')
export class Service {
  @PrimaryGeneratedColumn({ type: 'int' })
  id: number;

  @Column({
    type: 'varchar',
    comment: '用户主 id',
    nullable: false,
    default: () => `'${uuidv4().replace(/-/g, '')}'`,
  })
  primary_id: string;

  @Column({
    type: 'boolean',
    comment: '是否被删除',
    default: false,
  })
  isDelete: boolean;

  @Column({
    type: 'varchar',
    comment: '该服务的店铺ID',
  })
  shopNameId: string;

  @Column({
    type: 'varchar',
    comment: '服务类型',
    nullable: false,
    default: '通用类型',
  })
  category: string;

  @Column({
    type: 'varchar',
    comment: '类型',
    nullable: true,
    default: 'service',
  })
  type: string;

  @Column({ type: 'varchar', comment: '服务的描述', nullable: true })
  description: string;

  @OneToMany(() => ServicePhoto, (photo) => photo.service)
  photos: ServicePhoto[];

  @Column({
    type: 'varchar',
    comment: '服务的关键点',
  })
  service_point: string;

  // 发布这个服务的商家
  @ManyToOne(() => User, (user) => user.publish_service_id)
  shop: User;

  // 这个用户使用过的服务
  @ManyToMany(() => User, (user) => user.service)
  user: User[];

  // 用过这个服务的车
  @ManyToMany(() => Car, (car) => car.service)
  car: Car[];

  @OneToMany(() => Order, (order) => order.service)
  order: Order[];

  @OneToMany(() => Cart, (cart) => cart.service)
  cart: Cart[];

  @OneToMany(() => Geography, (geography) => geography.service)
  geography: Geography[];

  @Column({ type: 'int', comment: '服务的价格', default: 1, nullable: false })
  money: number;

  @Column({ type: 'varchar', comment: '该服务所在省', nullable: false })
  province: string;

  @Column({ type: 'varchar', comment: '该服务所在市', nullable: false })
  city: string;

  @Column({
    name: 'create_date',
    type: 'timestamp',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  create_date: Date;

  @Column({
    name: 'update_date',
    type: 'timestamp',
    comment: '修改时间',
    nullable: true,
  })
  updated_date: Date;
}
